package com.sgb.kjwl.view.ui.fragment.self;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMyPriceInfoBinding;
import com.sgb.kjwl.di.business.BusinessFragModule;
import com.sgb.kjwl.di.business.DaggerBusinessFragComponent;
import com.sgb.kjwl.viewmodel.self.publish.MyPriceInfoModel;
import com.swgk.core.base.di.AppComponent;

import javax.inject.Inject;

public class MyPriceInfoFragment extends AppFragment {

    private FragmentMyPriceInfoBinding binding;

    @Inject
    MyPriceInfoModel viewModel;

    //id
    private String id;


    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerBusinessFragComponent.builder().appComponent(appComponent).businessFragModule(new BusinessFragModule(this)).build().inject(this);
    }

    //id传递进来
    public static MyPriceInfoFragment newInstance(String  id) {
        MyPriceInfoFragment fragment = new MyPriceInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_my_price_info, container, false);
        binding.setViewModel(viewModel);
        return binding.getRoot();
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        id = (String) getArguments().get("id");
        initView();
        initData(id);
    }


    private void initView() {
        binding.tvPackUp.setText("收起");
        binding.tvPackUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.tvPackUp.getText().equals("收起")){
                    binding.tvPackUp.setText("展开");
                    binding.tvDesc.setSingleLine(true);
                    binding.tvDesc.setEllipsize(TextUtils.TruncateAt.END);
                }else {
                    binding.tvPackUp.setText("收起");
                    binding.tvDesc.setSingleLine(false);
                    binding.tvDesc.setEllipsize(null);
                    binding.tvDesc.setMaxLines(100);
                }
                binding.svFullContent.post(new Runnable() {
                    @Override
                    public void run() {
                        binding.svFullContent.fullScroll(View.FOCUS_DOWN);
                    }
                });
            }
        });

    }

    private void initData(String id) {

        viewModel.getData(id);
    }


}
